package cn.yufan.dao;

import cn.yufan.entity.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;

@Repository
public class AdminDao {

    @Autowired
    private JdbcTemplate template;
    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    /**
     * 根据条件查询
     * @param name      姓名
     * @param password  密码
     * @return
     */
    public Admin selectByNameAndPassword(String name, String password){
        MapSqlParameterSource sqlParameterSource=new MapSqlParameterSource();
        sqlParameterSource.addValue("name",name);
        sqlParameterSource.addValue("password",password);
        return namedParameterJdbcTemplate.queryForObject("select * from admin where name=:name and password=:password",
                sqlParameterSource,new BeanPropertyRowMapper<Admin>(Admin.class));
    }
    /**
     * 根据条件查询
     * @param name      姓名
     * @param telephone 电话号码
     * @return
     */
    public Admin selectByNameAndTelephone(String name,String telephone){
        Map<String,String> map=new HashMap<String,String>();
        map.put("name",name);
        map.put("telephone",telephone);
        return namedParameterJdbcTemplate.queryForObject("select * from admin where name=:name and telephone=:telephone",map,new BeanPropertyRowMapper<Admin>(Admin.class));
    }
}
